import request from '~/utils/http'

export const loginBgWeChatApi = (data: any, isLoading: boolean) => {
  const config = useRuntimeConfig()
  return request(`${config.public.apiBase}/ezsafe-server/wechat/getOpenId`, isLoading, {
    method: 'POST',
    data: data
  })
}

export const projectRegisterApi = (data: any, isLoading: boolean) => {
  const config = useRuntimeConfig()
  return request(`${config.public.apiBase}/ezsafe-server/projectRegister/save`, isLoading, {
    method: 'POST',
    data: data
  })
}

export const projectRegisterListApi = (data: any, isLoading: boolean) => {
  const config = useRuntimeConfig()
  return request(`${config.public.apiBase}/ezsafe-server/projectRegister/list`, isLoading, {
    method: 'POST',
    data: data
  })
}

export const enterpriseAssociationApi = (data: any, isLoading: boolean) => {
  const config = useRuntimeConfig()
  return request(`${config.public.apiBase}/ezsafe-server/akey/association/query`, isLoading, {
    method: 'POST',
    data: data
  })
}

export const enterpriseAssociationNewApi = (data: any, isLoading: boolean) => {
  const config = useRuntimeConfig()
  return request(`${config.public.apiBase}/ezsafe-server/wechat/associationQuery`, isLoading, {
    method: 'POST',
    data: data
  })
}

export const noticeListApi = (data: any, isLoading: boolean) => {
  const config = useRuntimeConfig()
  return request(`${config.public.apiBase}/ezsafe-server/notice/page`, isLoading, {
    method: 'POST',
    data: data
  })
}

export const noticeListNewApi = (data: any, isLoading: boolean) => {
  const config = useRuntimeConfig()
  return request(`${config.public.apiBase}/ezsafe-server/wechat/page`, isLoading, {
    method: 'POST',
    data: data
  })
}

export const projectRegisterRevokeApi = (XXBH: string, isLoading: boolean) => {
  const config = useRuntimeConfig()
  return request(`${config.public.apiBase}/ezsafe-server/projectRegister/revoke/${XXBH}`, isLoading, {
    method: 'PUT'
  })
}

export const projectRegisterRevokeNewApi = (XXBH: string, yzbs: string, isLoading: boolean) => {
  const config = useRuntimeConfig()
  return request(`${config.public.apiBase}/ezsafe-server/projectRegister/revokeYzjm/${XXBH}?yzbs=${yzbs}`, isLoading, {
    method: 'PUT'
  })
}

export const projectRegisterDetailApi = (XXBH: string, isLoading: boolean) => {
  const config = useRuntimeConfig()
  return request(`${config.public.apiBase}/ezsafe-server/projectRegister/detail/${XXBH}`, isLoading, {
    method: 'GET'
  })
}

export const projectRegisterDetailNewApi = (XXBH: string, yzbs: string, isLoading: boolean) => {
  const config = useRuntimeConfig()
  return request(`${config.public.apiBase}/ezsafe-server/projectRegister/info/${XXBH}?yzbs=${yzbs}`, isLoading, {
    method: 'GET'
  })
}

export const projectRegisterRectifyApi = (data: any, isLoading: boolean) => {
  const config = useRuntimeConfig()
  return request(`${config.public.apiBase}/ezsafe-server/projectRegister/saveZg`, isLoading, {
    method: 'POST',
    data: data
  })
}

export const noticeUnreadApi = (data: any, isLoading: boolean) => {
  const config = useRuntimeConfig()
  return request(`${config.public.apiBase}/ezsafe-server/notice/unReadCount`, isLoading, {
    method: 'POST',
    data: data
  })
}

export const noticeUnreadNewApi = (data: any, isLoading: boolean) => {
  const config = useRuntimeConfig()
  return request(`${config.public.apiBase}/ezsafe-server/wechat/unReadCount`, isLoading, {
    method: 'POST',
    data: data
  })
}

export const noticeUpdateApi = (XXBH: string, isLoading: boolean) => {
  const config = useRuntimeConfig()
  return request(`${config.public.apiBase}/ezsafe-server/notice/updateByXxbh/${XXBH}`, isLoading, {
    method: 'PUT'
  })
}

export const commonFileUploadNewApi = (file: any, yzbs: string) => {
  const config = useRuntimeConfig()
  return new Promise((resolve) => {
    // 注意：这里需要根据实际环境调整上传逻辑
    // 原代码使用了 mpvue.uploadFile，在 Nuxt 中需要适配
    const formData = new FormData()
    formData.append('file', file)
    formData.append('schema', 'mini')
    formData.append('yzbs', yzbs)
    
    fetch(`${config.public.apiBase}/ezsafe-server/wechat/commonUpload?schema=pc&compress=true&whiteList=true&yzbs=${yzbs}`, {
      method: 'POST',
      body: formData
    })
    .then(res => res.json())
    .then(resolve)
    .catch(resolve)
  })
}
